This projects aims to analyzes the correlation between planted trees and speed limits in Berlin. The goal is to see if there is a correlation between the two and if so, how strong it is.
Planted (streets-)trees in Berlin.
Important notes: Only speedlimits that are different from the default speed limit of 50km/h are listed. Therefor we need another datasource to get the streets with the default speed limit.
Important notes: We use this dataset (filtered for only (car-)streets) and inject the speed limits from datasource 2 into it, defaulting to 50km/h if no speed limit is found.
Is there a correlation between planted trees and speed limits in Berlin? If so, how strong is it and does the type of tree have an influence on the correlation?
Initially, install all required dependencies. We use requirements.txt to manage dependencies.
%%capture
%pip install -r requirements.txt
Create geopandas dataframes using the preprocessed data from the data pipeline. If some data is missing, the data pipeline will be executed again.
import geopandas as gpd
import os
if (not os.path.exists("data/trees.geojson")
or not os.path.exists("data/streets.geojson")
or not os.path.exists("data/speed_limits.geojson")
or not os.path.exists("data/merged_data.geojson")):
os.chdir("data")
os.system("python data/pipeline.py")
os.chdir("..")
trees: gpd.GeoDataFrame = gpd.read_file("data/trees.geojson")
streets: gpd.GeoDataFrame = gpd.read_file("data/streets.geojson")
speed_limits: gpd.GeoDataFrame = gpd.read_file("data/speed_limits.geojson")
merged_data: gpd.GeoDataFrame = gpd.read_file("data/merged_data.geojson")
We color all tree that where mapped to a street in green and trees that where not mapped to a street in red.
import plotly.io as pio
import plotly.express as px
import shapely
import numpy as np
pio.renderers.default = "notebook"
# Prepare data for plotting
unique_streets = merged_data[~merged_data["elem_nr"].duplicated(keep="last")]
trees_in_merged = trees[trees["id"].isin(merged_data["id_y"])]
trees_not_in_merged = trees[~trees["id"].isin(merged_data["id_y"])]
lats = []
lons = []
ids = []
names = []
speed_limits = []
for ident, feature, name, speed_limit in zip(unique_streets.id_x, unique_streets.geometry, unique_streets.strassenname, unique_streets.speed_limit):
if isinstance(feature, shapely.geometry.linestring.LineString):
linestrings = [feature]
elif isinstance(feature, shapely.geometry.multilinestring.MultiLineString):
linestrings = feature.geoms
else:
continue
for linestring in linestrings:
x, y = linestring.xy
lats = np.append(lats, y)
lons = np.append(lons, x)
ids = np.append(ids, [ident]*len(y))
names = np.append(names, [name]*len(y))
speed_limits = np.append(speed_limits, [speed_limit]*len(y))
lats = np.append(lats, None)
lons = np.append(lons, None)
ids = np.append(ids, None)
names = np.append(names, None)
speed_limits = np.append(speed_limits, None)
fig = px.line_mapbox(lat=lats, lon=lons, hover_name=names, hover_data=[ids, speed_limits], zoom=15, height=500, width=500)
scatter_trace_in_joined = px.scatter_mapbox(trees_in_merged,
lat=trees_in_merged.geometry.y,
lon=trees_in_merged.geometry.x,
color_discrete_sequence=['green'],
hover_name="gattung_deutsch",
hover_data=[],)
scatter_trace_not_in_joined = px.scatter_mapbox(trees_not_in_merged,
lat=trees_not_in_merged.geometry.y,
lon=trees_not_in_merged.geometry.x,
color_discrete_sequence=['red'],
hover_name="gattung_deutsch",
hover_data=[],)
for trace in scatter_trace_in_joined.data:
fig.add_trace(trace)
for trace in scatter_trace_not_in_joined.data:
fig.add_trace(trace)
fig.update_layout(mapbox_style="open-street-map", margin={"r":0,"t":0,"l":0,"b":0})
fig.show()